package com.debao.web.controller.debao;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.debao.common.constant.Constants;
import com.debao.system.domain.Goods;
import com.debao.system.domain.TimeUserCard;
import com.debao.system.domain.UserCourseCard;
import com.debao.system.service.ITimeUserCardService;
import com.debao.system.service.IUserCourseCardService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.debao.common.annotation.Log;
import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.enums.BusinessType;
import com.debao.system.domain.CourseCard;
import com.debao.system.service.ICourseCardService;
import com.debao.common.utils.poi.ExcelUtil;
import com.debao.common.core.page.TableDataInfo;

/**
 * 课程卡Controller
 * 
 * @author DeBao
 * @date 2024-06-13
 */
@RestController
@RequestMapping("/debao/card")
public class CourseCardController extends BaseController
{
    @Autowired
    private ICourseCardService courseCardService;

    @Autowired
    private ITimeUserCardService timeUserCardService;

    @Autowired
    private IUserCourseCardService userCourseCardService;

    /**
     * 查询课程卡列表
     */
    @PreAuthorize("@ss.hasPermi('debao:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(CourseCard courseCard)
    {
        startPage();
        List<CourseCard> list = courseCardService.selectCourseCardList(courseCard);
        return getDataTable(list);
    }

    /**
     * 导出课程卡列表
     */
    @PreAuthorize("@ss.hasPermi('debao:card:export')")
    @Log(title = "课程卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseCard courseCard)
    {
        List<CourseCard> list = courseCardService.selectCourseCardList(courseCard);
        ExcelUtil<CourseCard> util = new ExcelUtil<CourseCard>(CourseCard.class);
        util.exportExcel(response, list, "课程卡数据");
    }

    /**
     * 获取课程卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('debao:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(courseCardService.selectCourseCardById(id));
    }

    /**
     * 新增课程卡
     */
    @PreAuthorize("@ss.hasPermi('debao:card:add')")
    @Log(title = "课程卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseCard courseCard)
    {
        return toAjax(courseCardService.insertCourseCard(courseCard));
    }

    /**
     * 修改课程卡
     */
    @PreAuthorize("@ss.hasPermi('debao:card:edit')")
    @Log(title = "课程卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseCard courseCard)
    {
        return toAjax(courseCardService.updateCourseCard(courseCard));
    }

    /**
     * 课程上下线
     * @param id
     * @param online
     * @return
     */
    @PreAuthorize("@ss.hasPermi('debao:card:upStatus')")
    @GetMapping("/isOnline")
    public AjaxResult editStatus(@RequestParam Long id , @RequestParam Long online)
    {
        return toAjax(courseCardService.updateCourseCard(new CourseCard().setId(id).setOnline(online)));
    }

    /**
     * 删除课程卡
     */
    @PreAuthorize("@ss.hasPermi('debao:card:remove')")
    @Log(title = "课程卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
//        List<TimeUserCard> timeUserCards = timeUserCardService.selectTimeUserCardList(new TimeUserCard().setCourseId(id).setIsDel(Constants.IS_DEL_N));
//        List<UserCourseCard> userCourseCards = userCourseCardService.selectUserCourseCardList(new UserCourseCard().setCourseCardId(id).setIsDel(Constants.IS_DEL_N).setStatus(1));
//        if(timeUserCards.size() > 0 || userCourseCards.size() > 0){
//            return AjaxResult.error("该课程下有用户，不能删除");
//        }
        return toAjax(courseCardService.deleteCourseCardById(id));
    }
}
